﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using QLPKWeb.Models.LinQ.DanhMucChung;
using Webdiyer.WebControls.Mvc;
using DONGACS.Common;
using QLPKWeb.Models.FormBean.DanhMucChungModel;

namespace QLPKWeb.BussinessDelegate.DanhMucChung
{
    public class CompanyDelegate
    {
        DBDanhMucChungDataContext DBDanhMucChung = new DBDanhMucChungDataContext();

        public IList<dw_InformationCompany> GetAllCompany()
        {
            return DBDanhMucChung.dw_InformationCompanies.ToList();
        }

        public dw_InformationCompany AddNewCompany(dw_InformationCompany Company)
        {
            DBDanhMucChung.dw_InformationCompanies.InsertOnSubmit(Company);

            DBDanhMucChung.SubmitChanges();

            return GetCompanyIdMax();
        }

        public void DeleteCompany(string CompanyID)
        {
            dw_InformationCompany CompanyNew =
                DBDanhMucChung.dw_InformationCompanies.Single(p => p.Id == Convert.ToInt32(CompanyID));
            //Company.Id = Company.Id;

            DBDanhMucChung.dw_InformationCompanies.DeleteOnSubmit(CompanyNew);
            DBDanhMucChung.SubmitChanges();
        }

        public void UpdateCompany(dw_InformationCompany Company)
        {
            dw_InformationCompany CompanyUpdate =
            DBDanhMucChung.dw_InformationCompanies.Single(p => p.Id == Company.Id);
            CompanyUpdate.Id = Company.Id;
            CompanyUpdate.CompanyName = Company.CompanyName;
            CompanyUpdate.MaSoThue = Company.MaSoThue;
            CompanyUpdate.SoTaiKhoan = Company.SoTaiKhoan;
            CompanyUpdate.DiaChi = Company.DiaChi;
            CompanyUpdate.SoDT = Company.SoDT;
            CompanyUpdate.Fax = Company.Fax;
            CompanyUpdate.Mail = Company.Mail;
            CompanyUpdate.Website = Company.Website;

            DBDanhMucChung.SubmitChanges();
        }

        public PagedList<dw_InformationCompany> SearchCompanyPagedList(int? id, string searchCriteria)// id = page Index
        {
            //IQueryable<dm_Company> list = DBDanhMucChung.dm_Companys.Where(p => p.Ten == searchCriteria);
            //DBDanhMucChung.dm_Companys.ToPagedList(list,)

            return (PagedList<dw_InformationCompany>)DBDanhMucChung.dw_InformationCompanies.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage).Where(p => p.CompanyName == searchCriteria);

        }

        public dw_InformationCompany GetCompanyById(string id)
        {
            try
            {
                dw_InformationCompany item = this.DBDanhMucChung.dw_InformationCompanies.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dw_InformationCompany GetCompanyByTen(string ten)
        {
            //dm_Company item = this.DBDanhMucChung.dm_Companys.Where(p => p.Ten == ten).Count() != 0 ? this.DBDanhMucChung.dm_Companys.Where(p => p.Ten == ten).ElementAt(0) : null ;

            IEnumerable<dw_InformationCompany> list = this.DBDanhMucChung.dw_InformationCompanies.Where(p => p.CompanyName == ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }

        public PagedList<dw_InformationCompany> GetCompanyPagedList(int? id)// id = page Index
        {
            return DBDanhMucChung.dw_InformationCompanies.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dw_InformationCompany BuildNewCompany(CompanyForm form)
        {
            dw_InformationCompany newCompany = new dw_InformationCompany();
            newCompany.CompanyName = form.CompanyName;
            newCompany.SoTaiKhoan = form.SoTaiKhoan;
            newCompany.MaSoThue = form.MaSoThue;
            newCompany.DiaChi = form.DiaChi;
            newCompany.SoDT = form.SoDT;
            newCompany.Fax = form.Fax;
            newCompany.Mail = form.Mail;
            newCompany.Website = form.Website;

            return newCompany;
        }

        public dw_InformationCompany BuildEditCompany(CompanyForm form)
        {
            dw_InformationCompany newCompany = new dw_InformationCompany();
            newCompany.Id = form.Id;
            newCompany.CompanyName = form.CompanyName;
            newCompany.SoTaiKhoan = form.SoTaiKhoan;
            newCompany.MaSoThue = form.MaSoThue;
            newCompany.DiaChi = form.DiaChi;
            newCompany.SoDT = form.SoDT;
            newCompany.Fax = form.Fax;
            newCompany.Mail = form.Mail;
            newCompany.Website = form.Website;

            return newCompany;
        }

        public dw_InformationCompany GetCompanyIdMax()
        {
            IList<dw_InformationCompany> list = DBDanhMucChung.dw_InformationCompanies.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
    }
}